/********************************************************************
	created:	2008/09/08
	author:		Gary
	purpose:	
	email:		renjiahui@gmail.com
	site:		http://code.google.com/p/smsservice/
*********************************************************************/
// TCPClient.h: interface for the CTCPClient class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_TCPCLIENT_H__7BC75DAD_6259_4B87_9D2B_8A5726303B2C__INCLUDED_)
#define AFX_TCPCLIENT_H__7BC75DAD_6259_4B87_9D2B_8A5726303B2C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <stdio.h>
#include <winsock2.h>
#include <mswsock.h>
#include "ISocket.h"


class CTCPClient  : public ISocket
{
public:
	CTCPClient();
	virtual ~CTCPClient();
public:
    virtual BOOL  Connect(LPCTSTR pAddr, unsigned short wPort, DWORD timeout);
    virtual BOOL  Write(LPCTSTR WriteBuffer, ULONG size, ULONG& nRealWrite);
    virtual BOOL  Connect(SOCKADDR_IN remote_addr, DWORD timeout);
    BOOL  Read(CHAR* WriteBuffer, ULONG size,  ULONG& nRealRead);
	BOOL  Select(BOOL& bReadable, BOOL& bWriteable);
	BOOL  CloseSocket();	
    BOOL CreateServer(USHORT Port);	
public:
	void    Start();
	void    End();
public:
	BOOL SetOptLinger(SOCKET socket);
	void SetNonBlocking( SOCKET s,BOOL bNonBlocking );
private:
	int  CloseSocketSafe(SOCKET socket);

public:
	virtual VOID OnConnect();
	virtual VOID OnConnectFailed();
	virtual DWORD OnRead(LPCTSTR ReadBuffer, DWORD size, SOCKADDR_IN addr);
	virtual VOID OnConnectionBroken();
	virtual ISocket* OnAccept(SOCKET socket, SOCKADDR_IN* RemoteAddr, DWORD len);
public:
    LOG4CPLUS_CLASS_DECLARE( _s_logger );

};

#endif // !defined(AFX_TCPCLIENT_H__7BC75DAD_6259_4B87_9D2B_8A5726303B2C__INCLUDED_)
